<template>
  <view class="edit-avatar">
    <customHeader :disabled="isLoading" />
    <view class="edit-avatar-title" :style="titleTopBgStyle">
      <image :src="currentInfo.avatar" :lazy-load="true" />
      <!-- <view>{{ currentInfo.name }}</view> -->
    </view>
    <view class="avatar-main">
      <view
        class="avatar-wrap"
        v-for="item in avatarList"
        :key="item.id"
        :class="activeAvatar(item.id)"
        @tap="handleChangeAvatar(item)"
      >
        <image :src="item.avatar" :lazy-load="true" />
        <view>{{ item.name }}</view>
      </view>
    </view>
    <view class="upload">
      <view class="upload-wrap" @tap="handleSelectLocalImg">
        <text>都不喜欢，本地上传试试</text>
        <view class="right-icon" />
      </view>
    </view>
    <finishButton
      :width="690"
      :disabled="isLoading"
      @click="handleSelectAvatar"
    >
      保存
    </finishButton>
  </view>
</template>

<script>
import finishButton from "@/components/finish-button/index";
import customHeader from "@/components/custom-header";
import { getDefaultAvatar } from "@/libs/api/base-api";
import { hasBackgroundBgTitleStyle } from "@/libs/util";
import Taro from "@tarojs/taro";
import { EDIT_AVATAR_ICON } from "../image-url";
import { set } from "lodash";
export default {
  components: {
    customHeader,
    finishButton
  },
  data() {
    return {
      avatarList: [],
      currentInfo: {}
    };
  },
  computed: {
    titleTopBgStyle() {
      return hasBackgroundBgTitleStyle(EDIT_AVATAR_ICON, 76, 284);
    }
  },
  onLoad({ info }) {
    this.currentInfo = JSON.parse(info);
  },
  created() {
    this.handleGetDefaultAvatar();
  },
  methods: {
    handleSelectAvatar() {
      Taro.navigateBack({
        data: 1,
        success: () => {
          Taro.eventCenter.trigger("handleGetAvatar", this.currentInfo);
        }
      });
    },
    activeAvatar(id) {
      return { active: id === this.currentInfo.id };
    },
    handleChangeAvatar(info) {
      this.currentInfo = info;
    },
    /**获取默认头像 */
    async handleGetDefaultAvatar() {
      const { data = [] } = await getDefaultAvatar();
      this.avatarList = data;
    },
    handleSelectLocalImg() {
      Taro.chooseImage({
        count: 1,
        success: ({ tempFilePaths, tempFiles }) => {
          if (!this.uploadFileSizeIsLegal(tempFiles, { maxSize: 1 })) {
            Taro.showToast({
              title: "上传图片大小不超过1M",
              icon: "none"
            });
            return;
          }
          this.$startLoading("上传中");
          this.isLoading = true;
          const [midImg] = tempFilePaths;
          this.uploadFile(midImg, ({ code, url }) => {
            this.$endLoading();
            setTimeout(() => {
              this.isLoading = false;
            });
            if (code === 200) {
              this.$message("上传成功");
              this.currentInfo = { avatar: url };
              return;
            }
            this.$message("网络异常，请重新上传");
          });
        }
      });
    }
  }
};
</script>

<style lang="scss">
@import "@/assets/scss/tools.scss";
.edit-avatar {
  &-title {
    height: 400px;
    padding-top: 160px;
    text-align: center;
    image {
      width: 176px;
      height: 176px;
      border: 2px solid #ffffff;
      border-radius: 50%;
      box-sizing: border-box;
      margin-bottom: 16px;
    }
    view {
      @include base-text(44px, 32px, #303133);
    }
  }
  .avatar-main {
    padding: 124px 30px 0 40px;
    display: flex;
    flex-wrap: wrap;
    .avatar-wrap {
      margin-bottom: 54px;
      margin-right: 34px;
      image {
        width: 108px;
        height: 108px;
        border: 2px solid #ffffff;
        border-radius: 50%;
        margin-bottom: 12px;
        box-sizing: border-box;
      }
      view {
        text-align: center;
        max-width: 108px;
        @include textEllipsis;
        @include base-text(40px, 28px, #303133, 400);
      }
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
    .active {
      image {
        border-color: #ff8000;
      }
      view {
        color: #ff8000;
      }
    }
  }
  .upload {
    position: fixed;
    bottom: 152px;
    width: 100%;
    @include normal-flex();
    @include base-text(40px, 28px, #606266, 400);
    &-wrap {
      @include normal-flex();
    }
    .right-icon {
      width: 32px;
      height: 32px;
      @include setBgi("~@/assets/icon/right.png");
    }
  }
  .finish-button {
    position: fixed;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
  }
  .custom-header {
    position: absolute;
    top: 0;
  }
}
</style>
